package com.penghaisoft.microwms.materialhand.web.insidewarehouse.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.HandBaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.RawCountingDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.wms.model.RawCountingDifference;
import com.penghaisoft.microwms.common.wms.model.RawCountingRecords;
import com.penghaisoft.microwms.common.wms.model.RawCountingTask;
import com.penghaisoft.microwms.common.wms.model.RawStorageSn;
import com.penghaisoft.microwms.materialhand.web.insidewarehouse.service.IMaterialHandInsideWhService;


/** 
* @ClassName: CountingMaterialController 
* @Description: 原材料盘点
* @author jzh
* @date 2018年5月25日 下午4:23:15  
*/
@RestController
@RequestMapping("insideWh/counting")
public class CountingMaterialController extends HandBaseController {

    @Autowired
    private IMaterialHandInsideWhService materialHandInsideWhService;
    
    /** 
    * @Title: countingCodeExist 
    * @Description: 判断盘点单号是否存在
    * @param @param rawBackDto
    * @param @return  参数说明 
    * @return HandResp    返回类型
    * @author jzh 
    * @throws 
    */
    @PostMapping("countingcode")
    public HandResp countingCodeExist(@RequestBody RawCountingDto rawCountingDto){
        HandResp result = new HandResp();
        //Code先置为1SUCCESS
        result.setCode(Constant.SUCCESS);
        if (rawCountingDto.getCountingCode()==null||rawCountingDto.getCountingCode().equals("")){
            result.setCode(Constant.FAIL);
            result.setMsg("请输入盘点单号！");
            return result;
        }
        //设置查询标志（CountingCode=？ ，ActiveFlag = 1）
        RawCountingTask rawCountingTask = new RawCountingTask();
        rawCountingTask.setCountingCode(rawCountingDto.getCountingCode());
        rawCountingTask.setActiveFlag("1");
       
        List<RawCountingTask> tmp = materialHandInsideWhService.queryRawCountingTask(rawCountingTask);
        if(tmp.size() > 0){
        	if(tmp.get(0).getStauts().equals("0")){
        		result.setMsg("盘点任务存在，请盘点");
        		//记录last_modified_by，gmt_modified	最后修改人，最后修改时间
        		rawCountingTask.setLastModifiedBy(rawCountingDto.getLoginName());
        		rawCountingTask.setGmtModified(new Date());
        		//根据countingTaskId修改
        		rawCountingTask.setCountingTaskId(tmp.get(0).getCountingTaskId());
        		Resp resp = new Resp();
        		//将盘点任务状态置为盘点中
        		resp = materialHandInsideWhService.updateRawCountingTask(rawCountingTask);
        		result.setCode(resp.getCode());
        		if (resp.getMsg() != null && !resp.getMsg().equals("")) {
        			result.setMsg(resp.getMsg());
        		}
        	}else if(tmp.get(0).getStauts().equals("1")){
        		result.setCode(Constant.SUCCESS);
            	result.setMsg("盘点任务正在盘点中");
        	}else if(tmp.get(0).getStauts().equals("2")){
        		result.setCode(Constant.FAIL);
            	result.setMsg("盘点任务已完成");
        	}else{
        		result.setCode(Constant.FAIL);
        		result.setMsg("盘点任务未知状态");
        	}
        }else{
        	result.setCode(Constant.FAIL);
        	result.setMsg("盘点任务不存在！");
        }
        
        return result;
    }
    
    /** 
    * @Title: barcodeExist 
    * @Description: 扫码盘点
    * @author jzh 
    */
    @PostMapping("barcodeisexist")
    public HandResp barcodeExist(@RequestBody RawCountingDto rawCountingDto){
        HandResp result = new HandResp();
        //入参条码，盘点单号，LoginName
        String barcode= rawCountingDto.getBarcode();
        String countingCode = rawCountingDto.getCountingCode();
        String LoginName = rawCountingDto.getLoginName();
        //根据盘点单号查询合同号
        List<RawCountingTask> tmp = materialHandInsideWhService.queryRawCountingTaskByCode(countingCode);
        String contractNo = tmp.get(0).getContractNo();
        //根据条码，合同号到rawStorageSn中查询，判断当前条码是否在合同号下
        RawStorageSn rawStorageSn = new RawStorageSn();
        rawStorageSn.setBarcode(barcode);
        rawStorageSn.setContractNo(contractNo);
        rawStorageSn.setActiveFlag("1");
        List<RawStorageSn> list = materialHandInsideWhService.querybarcodeExist(rawStorageSn);
        if(list.size()>0){
        	//当前条码存在合同号下
        	RawCountingRecords rawCountingRecords = new RawCountingRecords();
        	rawCountingRecords.setCountingCode(countingCode);
        	rawCountingRecords.setBarcode(barcode);
        	//查看这个条码是否已经被盘点了countingCode barcode
        	List<RawCountingRecords> rawCountingRecordsList =materialHandInsideWhService.queryRawCountingRecordsExist(rawCountingRecords);
        	if(rawCountingRecordsList.size()>0){
        		result.setCode(Constant.FAIL);
            	result.setMsg("改条码已经被盘点了，请继续扫描");
        	}else{
        		//创建盘点记录
		    	rawCountingRecords.setCountingTime(new Date());
		    	rawCountingRecords.setModel(list.get(0).getModel());
		    	rawCountingRecords.setModelDesc(list.get(0).getModelDesc());
		    	rawCountingRecords.setModelType(list.get(0).getModelType());
		    	rawCountingRecords.setWhCode(list.get(0).getWlCode());
		    	rawCountingRecords.setWdCode(list.get(0).getWdCode());
		    	rawCountingRecords.setWaCode(list.get(0).getWaCode());
		    	rawCountingRecords.setWlCode(list.get(0).getWlCode());
		    	rawCountingRecords.setKeepBy(LoginName);
		    	rawCountingRecords.setCreateBy(LoginName);
		    	rawCountingRecords.setGmtCreate(new Date());
		    	rawCountingRecords.setActiveFlag("1");
		    	Resp resp = materialHandInsideWhService.createRawCountingRecords(rawCountingRecords);
		    	//返回手持页面信息（当前扫描条码信息）
		        List Countinglist = new ArrayList<>();
		      
		        rawCountingDto.setModel(list.get(0).getModel());
		        rawCountingDto.setModelDesc(list.get(0).getModelDesc());
		        rawCountingDto.setModelType(list.get(0).getModelType());
		        rawCountingDto.setWhCode(list.get(0).getWlCode());
		        rawCountingDto.setWdCode(list.get(0).getWdCode());
		        rawCountingDto.setWaCode(list.get(0).getWaCode());
		        rawCountingDto.setWlCode(list.get(0).getWlCode());
		        rawCountingDto.setAmount(list.get(0).getAmount());
		        rawCountingDto.setKeepBy(LoginName);
		        //当前盘点数量
		        int nowCountingNum = 0;
		        //根据盘点单号在盘点记录表中查询已盘点数量
		        nowCountingNum = materialHandInsideWhService.querCountByCountingCode(countingCode);
		        rawCountingDto.setNowCountingNum(nowCountingNum);
		        Countinglist.add(rawCountingDto);
	            result.setList(Countinglist);
	            result.setCode(resp.getCode());
	            result.setMsg(resp.getMsg());
        	}
        
        }else{
        	result.setCode(Constant.FAIL);
        	result.setMsg("当前条码不是该合同的条码，请继续扫描");
        }
        return result;
    }
    
    /** 
    * @Title: countingConfig 
    * @Description: 盘点确认
    * @param @param rawCountingDto
    * @param @return  参数说明 
    * @return HandResp    返回类型
    * @author jzh 
    * @throws 
    */
    @PostMapping("countingconfig")
    public HandResp countingConfig(@RequestBody RawCountingDto rawCountingDto){
        HandResp result = new HandResp();
        //入参：盘点单号，LoginName
        String countingCode = rawCountingDto.getCountingCode();
        String LoginName = rawCountingDto.getLoginName();
        //根据盘点单号查询合同号
        List<RawCountingTask> tmp = materialHandInsideWhService.queryRawCountingTaskByCode(countingCode);
        if(tmp.size()>0){
	        String contractNo = tmp.get(0).getContractNo();
	        rawCountingDto.setContractNo(contractNo);
	        //根据盘点单号，合同号 查询盘亏数据
	        List<RawStorageSn> listLose = materialHandInsideWhService.queryLose(rawCountingDto);
	        if(listLose.size()>0){
	        	//盘亏，存在盘亏数据
	        	for(RawStorageSn rawStorageSn:listLose){
	        		//创建盘亏差异记录
	        		RawCountingDifference rawCountingDifference = new RawCountingDifference();
	        		rawCountingDifference.setCountingCode(countingCode);
	        		rawCountingDifference.setContractNo(contractNo);
	        		rawCountingDifference.setPurchaseNo(rawStorageSn.getPurchaseNo());
	        		rawCountingDifference.setCountingTime(new Date());//盘点时间记录当前时间了
	        		rawCountingDifference.setProBarcode(rawStorageSn.getBarcode());
	        		rawCountingDifference.setModel(rawStorageSn.getModel());
	        		rawCountingDifference.setModelDesc(rawStorageSn.getModelDesc());
	        		rawCountingDifference.setDifferType("1");//盘亏
	        		rawCountingDifference.setDifferStatus("0");//未处理
	        		rawCountingDifference.setWaCode(rawStorageSn.getWaCode());
	        		rawCountingDifference.setWdCode(rawStorageSn.getWdCode());
	        		rawCountingDifference.setWhCode(rawStorageSn.getWhCode());
	        		rawCountingDifference.setWlCode(rawStorageSn.getWlCode());
	        		rawCountingDifference.setKeepBy(LoginName);//保管员记录为当前登录人了
	        		rawCountingDifference.setGmtCreate(new Date());
	        		rawCountingDifference.setActiveFlag("1");
	        		rawCountingDifference.setCreateBy(LoginName);
	        		Resp resp = materialHandInsideWhService.createRawCountingDifference(rawCountingDifference);	
	        	}
	        }
	      //根据盘点单号，合同号 查询盘盈数据
	        List<RawCountingRecords> listWin = materialHandInsideWhService.queryWin(rawCountingDto);
	        if(listWin.size()>0){
	        	for(RawCountingRecords rawCountingRecords : listWin){
	        		//创建盘盈差异记录
	        		RawCountingDifference rawCountingDifference = new RawCountingDifference();
	        		rawCountingDifference.setCountingCode(countingCode);
	        		rawCountingDifference.setContractNo(contractNo);
	        		//rawCountingDifference.setPurchaseNo(rawCountingRecords.getPurchaseNo());
	        		rawCountingDifference.setCountingTime(new Date());//盘点时间记录当前时间了
	        		rawCountingDifference.setProBarcode(rawCountingRecords.getBarcode());
	        		rawCountingDifference.setModel(rawCountingRecords.getModel());
	        		rawCountingDifference.setModelDesc(rawCountingRecords.getModelDesc());
	        		rawCountingDifference.setDifferType("0");//盘盈
	        		rawCountingDifference.setDifferStatus("0");//未处理
	        		rawCountingDifference.setWaCode(rawCountingRecords.getWaCode());
	        		rawCountingDifference.setWdCode(rawCountingRecords.getWdCode());
	        		rawCountingDifference.setWhCode(rawCountingRecords.getWhCode());
	        		rawCountingDifference.setWlCode(rawCountingRecords.getWlCode());
	        		rawCountingDifference.setKeepBy(LoginName);//保管员记录为当前登录人了
	        		rawCountingDifference.setGmtCreate(new Date());
	        		rawCountingDifference.setActiveFlag("1");
	        		rawCountingDifference.setCreateBy(LoginName);
	        		Resp resp = materialHandInsideWhService.createRawCountingDifference(rawCountingDifference);	
	        	}
	        }
	        RawCountingTask rawCountingTask = new RawCountingTask();
	        rawCountingTask.setLastModifiedBy(rawCountingDto.getLoginName());
			rawCountingTask.setGmtModified(new Date());
			//根据countingTaskId修改
			rawCountingTask.setCountingTaskId(tmp.get(0).getCountingTaskId());
			Resp resp = new Resp();
			//将盘点任务状态置为盘点结束
			resp = materialHandInsideWhService.updateRawCountingTaskFinish(rawCountingTask);
			
	        if(listWin.size()==0&&listLose.size()==0){
	        	result.setCode(Constant.SUCCESS);
	        	result.setMsg("盘点无差异");
	        }else{
	        	result.setCode(Constant.SUCCESS);
	        	result.setMsg("盘点差异已记录");
	        }
        }else{
        	result.setCode(Constant.FAIL);
        	result.setMsg("盘点单号不存在");
        	
        }
         
        return result;
    }
    
}
